package com.coda.controller;

import com.coda.adminVo.attr.AttrKeyVo;
import com.coda.adminVo.attr.AttrValueVo;
import com.coda.entity.AttrKey;
import com.coda.entity.AttrValue;
import com.coda.result.Result;
import com.coda.service.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/attr")
@Api(tags = "属性管理")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 新增或更新属性值
     */
    @PostMapping("/value/saveOrUpdate")
    @ApiOperation(value = "新增或更新属性")
    public Result saveOrUpdateValue(@RequestBody AttrValue attrValue){
        attrService.saveOrUpdateValue(attrValue);
        return Result.ok();
    }
    /**
     * 新增或更新属性名称
     */
    @PostMapping("/key/saveOrUpdate")
    @ApiOperation(value = "新增或更新属性名称")
    public Result saveOrUpdate(@RequestBody AttrKey attrKey){
        attrService.saveOrUpdateKey(attrKey);
        return Result.ok();
    }
    /**
     * 查看全部属性名称和属性值列表
     */
    @GetMapping("/list")
    @ApiOperation("查看全部属性名称和属性值列表")
    public Result<List<AttrKeyVo>> list(){
        List<AttrKeyVo> attrKeyVos = attrService.list();
        return Result.ok(attrKeyVos);

    }
    /**
     * 根据id删除属性值
     */
    @DeleteMapping("/value/deleteById")
    @ApiOperation(value = "根据id删除属性值")
    public Result deleteValueById(Long id){
        attrService.deleteValueById(id);
        return Result.ok();
    }
    /**
     * 根据id删除名称
     */
    @DeleteMapping("/key/deleteById")
    @ApiOperation(value = "根据id删除名称")
    public Result deleteKeyById(@RequestParam Long attrKeyId){
        attrService.deleteKeyById(attrKeyId);
        return Result.ok();
    }

}
